<?php

namespace app\models;

use think\Model;

/**
 * Class CampusModel.
 */
class StudentSubjectClassModel extends Model
{
    protected $table = 'edu_class_subject_student';

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     *
     * @return string
     */
    public static function getIds()
    {
        $campus = self::select()->toArray();
        $arr = [];
        foreach ($campus as $cap) {
            $arr[] = $cap['id'];
        }

        return implode(',', $arr);
    }

    /**
     * @param $id
     * @param string $type
     *
     * @throws \think\exception\DbException
     *
     * @return null|array|AuthGroupAccessModel
     */
    public static function getById($id, $type = 'all')
    {
        if (!is_numeric($id)) {
            return [];
        }
        $campus = self::get(['id' => $id]);
        if ($campus) {
            return (isset($campus[$type])) ? $campus[$type] : $campus;
        }

        return [];
    }

	/**
	 * modify by Small Y
	 *
	 * @param $param
	 * @param $id
	 * @param $room_id
	 * @param $campus_id
	 * @return array
	 * @throws \think\exception\DbException
	 */
    public function getStudentLists($param, $id , $room_id,$campus_id)
    {
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
        $where = ["class_subject_id" => $id, 'room_id' => $room_id,'campus_id'=>$campus_id];
        $subject_class_result = GradesDetailsModel::get(['id' => $id]);  //排课课程表
        $result = ClassRoomModel::get(['id' => $room_id]);
        $subject['campus_name'] = CampusModel::get(['id' => $subject_class_result['campus_id']])['campus_name'];//校区名称
        $subject['class_room_name'] = $result['class_name'];//教室名称
        $subject['course_name'] = CourseModel::get(['id' => $subject_class_result['course_id']])['course_name']; //课程名称
        $seat_num = $result['seat_nums'].""; //座位数
        $length = strlen($seat_num);
        $listData = self::where($where)
            ->paginate($listRows, false, ['page' => $page])
            ->toArray();
        if (!empty($listData)) {
            foreach ($listData['data'] as &$item) {
                $item['campus_name'] = $subject['campus_name'];
                $item['class_room_name'] =  $subject['class_room_name'];
                $item['course_name'] = $subject['course_name'];
                $number_len = strlen($item['seat_number']."");
                $str_ = "";
                for($p=0; $p< $number_len; $p++){
                    $str_ = $str_+"0";
                }
                $value = $str_.$item['seat_number'];
                $value = '锁定编号'.'('.$value.')';
                if($item['seat_number'] == NULL){
                    $item['status_text']  =  '<span  style="color:#FF5722;font-weight: bold;">未选中</span>';
                }else{
                    $item['status_text']  =   '<span style="color:#01AAED;font-weight: bold;">'.$value.'</span>';
                }
            }
        }
        return $listData;
    }

    public function getGradesStudentsLists($param){
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
        $where = [];
        $course_id = GradesDetailsModel::get(['id' => $param['class_subject_id']])['course_id']; //课程ID
        //班级id
        if (isset($param['class_subject_id']) && '' !== $param['class_subject_id']) {
            $where['class_subject_id'] = $param['class_subject_id'];
        }

        //room_id
        if (isset($param['room_id']) && '' !== $param['room_id']) {
            $where['room_id'] = $param['room_id'];
        }
		if(get_current_type() == 4 || get_current_type() == 5){
			$where['campus_id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){
			$where['campus_id'] = ['in',get_current_campus_id()];
		}
        $listData = self::where($where)
            ->paginate($listRows, false, ['page' => $page])
            ->toArray();
        $listData_ = [];
        $student_arr = [];
        $CourseContractModel = new CourseContractModel();
        $GradesContractModel = new GradesContractModel();
        $StudentSubjectInfosClassModel = new StudentSubjectInfosClassModel();
        if (!empty($listData)) {
            foreach ($listData['data'] as &$item) {
                if(!in_array($item['student_id'],$student_arr)){
                    $item_ = [];
					$item_['class_nums'] = '';
                    $item_['student_name'] = $item['student_name'];
                    $res = StudentModel::get(['id' => $item['student_id']]);
                    if($res['is_audition'] == 0 && $res['sign_up'] == 0){
                        $item_['student_type_text'] = '试听学员';
                        $item_['pay_status_text'] = '-';
                        $item_['class_nums'] = '-';
                    }else if($res['is_audition'] == 2 && $res['sign_up'] == 1){
                        $result = self::get(['student_id' =>  $item['student_id'], 'student_type' => 1]);
                        if($result){
                                $item_['student_type_text'] = '正式学员';
                                $course_res = ContractModel::get(['contract_number' => $result['contract_number'], 'student_id' => $result['student_id']]);
                                if($course_res['status'] == 1){  //已收款合同：1，待收款合同：2 ,部分收款合同：
                                    $item_['pay_status_text'] = '已收款';
                                }else if($course_res['status'] == 2){
                                    $item_['pay_status_text'] = '待收款';
                                }else if($course_res['status'] == 3){
                                    $item_['pay_status_text'] = '部分收款';;
                                }

                            $num = $CourseContractModel->where(['student_id' => $result['student_id'], 'course_id' => $course_id])->sum('class_num');
                            $num_ = $GradesContractModel->where(['student_id' => $result['student_id'], 'course_id' => $course_id])->sum('class_num');
                            $item_['class_nums'] = $num + $num_;  //购买总课次
                        }else{
                            if($item['student_type'] == 2){
                                $item_['student_type_text'] = '临时学员';
                                $item_['pay_status_text'] = '-';
                                $item['class_nums'] = '-';
                            }else if($item['student_type'] == 3){
                                $item_['student_type_text'] = '补课学员';
                                if($item['contract_number'] != '---') {
                                    $course_res = ContractModel::get(['contract_number' => $item['contract_number'], 'student_id' => $item['student_id']]);
                                    if ($course_res['status'] == 1) {  //已收款合同：1，待收款合同：2 ,部分收款合同：
                                        $item_['pay_status_text'] = '已收款';
                                    } else if ($course_res['status'] == 2) {
                                        $item_['pay_status_text'] = '待收款';
                                    } else if ($course_res['status'] == 3) {
                                        $item_['pay_status_text'] = '部分收款';;
                                    }
                                    $num = $CourseContractModel->where(['student_id' => $item['student_id'], 'course_id' => $course_id])->sum('class_num');
                                    $num_ = $GradesContractModel->where(['student_id' => $item['student_id'], 'course_id' => $course_id])->sum('class_num');
                                    $item_['class_nums'] = $num + $num_;  //购买总课次
                                }else{
                                    $item_['pay_status_text'] = '-';
                                    $item['class_nums'] = '-';
                                }
                            }
                        }
                    }
                    $class_date_max = $StudentSubjectInfosClassModel->where(['course_id' => $course_id, 'class_subject_id' => $item['class_subject_id'],  'student_id' => $item['student_id'], 'attendance' => 1, 'status'=>1])->order('class_date desc')->find();
                    if($class_date_max){
                        $item_['class_date_max'] = $class_date_max['class_date'];  //最近到课日期
                    }else{
                        $item_['class_date_max'] = '-';  //最近到课日期
                    }
                    $item_['class_arrangement_num'] = $StudentSubjectInfosClassModel->where(['course_id' => $course_id, 'class_subject_id' => $item['class_subject_id'], 'student_id' => $item['student_id']])->count(); //排课总课次
                    $nums_surplus = $StudentSubjectInfosClassModel->where(['course_id' => $course_id, 'class_subject_id' => $item['class_subject_id'], 'student_id' => $item['student_id'], 'charging' => 1, 'status'=>1])->count();      //课消次数
                    if($item_['class_nums'] != '-'){
                            if($item_['class_nums'] >= $nums_surplus){
                                $item_['class_arrangement_surplus'] = $item_['class_nums'] - $nums_surplus;
                            }else{
                                $item_['class_arrangement_surplus'] = 0;
                            }
                    }else if($item_['class_nums'] == '-'){
                            if($item_['class_arrangement_num'] >= $nums_surplus){
                                $item_['class_arrangement_surplus'] = $item_['class_arrangement_num'] - $nums_surplus;
                            }else{
                                $item_['class_arrangement_surplus'] = 0;
                            }
                    }
                    $listData_['data'][] = $item_;
                    $student_arr[] = $item['student_id'];
                }

                /*
                if($item['student_type'] == 1){  //学员
                    $item['student_type_text'] = '正式学员';
                }else if($item['student_type'] == 2){   //临时学员
                    $item['student_type_text'] = '临时学员';
                }else if($item['student_type'] == 3){  //补课学员
                    $item['student_type_text'] = '补课学员';
                }else if($item['student_type'] == 4){ //试听学员
                    $item['student_type_text'] = '试听学员';
                }

                $contract_result = $ContractModel->where(['student_id' => $item['student_id']])->select();
                $pay_status = '';
                $class_nums = 0;
                foreach ($contract_result as $key => $value){
                    $course_num_arr = explode(',',$value['course_nums']);
                    $course_id_arr = explode(',',$value['course']);
                    for($i =0; $i < count($course_num_arr); $i ++){
                        if($course_id_arr[$i] == $course_id){  //判断课程是否是所查课程
                            $class_nums = $class_nums + $course_num_arr[$i];
                        }

                    }
                    if($value['status'] == 1){
                        $pay_status = $pay_status.'【'.$value['contract_number'].''.'已收款】';
                    }else if($value['status'] == 2){
                        $pay_status = $pay_status.'【'.$value['contract_number'].''.'待收款】';
                    }else if($value['status'] == 3){
                        $pay_status = $pay_status.'【'.$value['contract_number'].''.'部分收款】';
                    }
                }
                $item['pay_status_text'] = $pay_status;
                $class_date_max = $StudentSubjectInfosClassModel->where(['course_id' => $course_id, 'class_subject_id' => $item['class_subject_id'],  'student_id' => $item['student_id'], 'campus_id' =>  get_current_campus_id(), 'attendance' => 1, 'status'=>1])->order('class_date desc')->find();
                $item['class_date_max'] = $class_date_max['class_date'];  //最近到课日期
                $item['class_nums'] = $class_nums;  //购买总课次
                $item['class_arrangement_num'] = $StudentSubjectInfosClassModel->where(['course_id' => $course_id, 'class_subject_id' => $item['class_subject_id'], 'student_id' => $item['student_id'], 'campus_id' =>  get_current_campus_id()])->count(); //排课总课次


                $nums_surplus = $StudentSubjectInfosClassModel->where(['course_id' => $course_id, 'class_subject_id' => $item['class_subject_id'], 'student_id' => $item['student_id'], 'campus_id' =>  get_current_campus_id(), 'charging' => 1, 'status'=>1])->count();      //课消次数

                $item['class_arrangement_surplus'] = ($class_nums - $nums_surplus);
                */

            }
        }
        return $listData_;
    }
}
